<?php
class User extends AppModel{
	var $name = 'User';
	// This is used for validation, see Chapter "Data Validation".
	var $validate = array(
		'username' => array(
			"notEmpty"=>array(
				"rule"=>array("notEmpty"),
				"message" => "使用者名稱不能為空"
			),
			"alphaNumeric"=>array(
				"rule"=>array("alphaNumeric"),
				"message" => "使用者名稱只能包含英數字元"
			),
			"isUnique"=>array(
				"rule"=>array("isUnique"),
				"message" => "使用者名稱已經被註冊過了"
			)
		),
		'email' => array(
			"notEmpty"=>array(
				"rule"=>array("notEmpty"),
				"message" => "Email不能為空"
			),
			"email"=>array(
				"rule"=>array("email"),
				"message" => "不是正確的Email地址"
			),
			"emailUnique"=>array(
				"rule"=>array("isUnique"),
				"message" => "Email已經被註冊過了"
			)
		),
		"student_id" => array(
			"notEmpty"=>array(
				"rule"=>array("notEmpty"),
				"message" => "學號不能為空"
			),
			"student_id"=>array(
				"rule"=>array("validStudent"),
				"message" => "不是正確的學號"
			),
			"isUnique"=>array(
				"rule"=>array("isUnique"),
				"message" => "學號已經被註冊過了"
			)
		),
		"nickname" => array(
			"notEmpty"=>array(
				"rule"=>array("notEmpty"),
				"message" => "暱稱不能為空"
			)
		)
	);
	function validStudent($data){
		$value = array_values($data);
		$value = $value[0];
		return preg_match('/^[bBrRdD]{1}[0-9]{2}[ABab0-9]{1}[0-9]{5}$/i', $value);
	}
	function emailUnique($data){
		$value = array_values($data);
		$value = $value[0];
		if($this->id){
			echo($this->find('count',array("conditions"=>array("User.email"=>$value,"User.id <>"=>$this->id))));
			return $this->find('count',array("conditions"=>array("User.email"=>$value,"User.id <>"=>$this->id)))==0;
		}else{
			return $this->find('count',array("conditions"=>array("User.email"=>$value)))==0;
		}
	}
	var $belongsTo = array('Group');
	var $actsAs = array('Acl' => 'requester');
	function parentNode() {
		if (!$this->id && empty($this->data)) {
			return null;
		}
		$data = $this->data;
		if (empty($this->data)) {
			$data = $this->read();
		}
		if(isset($data['User']['group_id'])){
			if (!$data['User']['group_id']) {
				return null;
			} else {
				return array('Group' => array('id' => $data['User']['group_id']));
			}
		}
	}
	function afterSave($created) {
		if (!$created) {
			$parent = $this->parentNode();
			$parent = $this->node($parent);
			$node = $this->node();
			$aro = $node[0];
			$aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
			$this->Aro->save($aro);
		}
	}
	/*
	 *
	 * activate ($id, $code) - Activate account with code.
	 *
	 * param:
	 *
	 * $id : user id
	 *
	 * $code : activation code
	 *
	 * return:
	 *
	 * true if success, false if fail.
	 *
	 */
	function activate($id,$code){
			$user=$this->findById($id);
			if(empty($user)){
				throw new Exception("No such user.");
			}
			if($user["User"]["act_code"]!=$code){
				throw new Exception("Invalid code.");
			}
			$this->id=$id;
			$this->set('activated', 1);
			return $this->save();
	}
	function update_ip($id,$ip){
			$this->id=$id;
			$this->set('last_ip', $ip);
			return $this->save();
	}
}
?>